package cj.web.admin.controllers;

import java.util.EnumSet;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.utils.Constants;
import cj.utils.ReturnUUID;
import cj.web.admin.dao.AreaDAO;
import cj.web.admin.dao.ComposeOnlineOrderDAO;
import cj.web.admin.dao.ComposeSubjectDAO;
import cj.web.admin.dao.GameDAO;
import cj.web.admin.dao.GradeDAO;
import cj.web.admin.dao.SchoolDAO;
import cj.web.admin.domain.Area;
import cj.web.admin.domain.ComposeOnlineOrder;
import cj.web.admin.domain.ComposeSubject;
import cj.web.admin.domain.Game;
import cj.web.admin.domain.GameOrder;
import cj.web.admin.domain.Grade;
import cj.web.constants.GameTypeEnum;
import cj.web.constants.IsCheckEnum;
import cj.web.constants.IsExamEnum;
import cj.web.constants.IsWinEnum;
import cj.web.constants.MemuEnum;
import cj.web.constants.OrderHeardEnum;
import cj.web.constants.OrderSourceEnum;
import cj.web.constants.PayStatusEnum;
import cj.web.constants.PayWayEnum;
import cj.web.login.controllers.BaseController;

@Path("/onlineOrder")
public class COnlineOrderController extends BaseController{

	private static Logger logger = Logger.getLogger(COnlineOrderController.class.getName());
	
	@Autowired
	private ComposeOnlineOrderDAO composeOnlineOrderDao;
	@Autowired
	private GradeDAO gradeDao;
	@Autowired
	private AreaDAO areaDao;
	@Autowired
	private SchoolDAO schoolDao;
	@Autowired
	private GameDAO gameDao;
	@Autowired
	private ComposeSubjectDAO composeSubjectDAO;
	
	@Get("show")
	public String show(Model model,ComposeOnlineOrder composeOnlineOrder) {
		List<Map> grades = gradeDao.asGradeEnum();
		model.add("Grade", grades);
		List<Map> areas = areaDao.asAreaEnum();
		model.add("Area", areas);
		List<Map> schools = schoolDao.asSchoolEnum();
		model.add("School", schools);
		List<Map> games = gameDao.asGameEnum(Integer.parseInt(GameTypeEnum.WLZZ.getValue()));
		model.add("Game", games);
		model.add("PayStatus", EnumSet.allOf(PayStatusEnum.class));
		model.add("IsCheckStatus", EnumSet.allOf(IsCheckEnum.class));
		model.add("IsExamStatus", EnumSet.allOf(IsExamEnum.class));
		model.add("IsWinStatus", EnumSet.allOf(IsWinEnum.class));
		//MemuEnum
        model.add("MemuEnum",MemuEnum.wlzwcsxsgl);
		return "/views/online/onlineOrderList.vm";
	}
	
	/**
	 * 参赛选手信息修改页面
	 * @param model
	 * @param gameOrder
	 * @return
	 */
	@Get("updateShow")
	public String updateShow(Model model,GameOrder gameOrder) {
		List<Map> grades = gradeDao.asGradeEnum();
		model.add("Grade", grades);
		List<Map> areas = areaDao.asAreaEnum();
		model.add("Area", areas);
		List<Map> schools = schoolDao.asSchoolEnum();
		model.add("School", schools);
		List<Map> games = gameDao.asGameEnum(Integer.parseInt(GameTypeEnum.WLZZ.getValue()));
		model.add("Game", games);
		model.add("PayStatus", EnumSet.allOf(PayStatusEnum.class));
		model.add("IsCheckStatus", EnumSet.allOf(IsCheckEnum.class));
		model.add("IsExamStatus", EnumSet.allOf(IsExamEnum.class));
		model.add("IsWinStatus", EnumSet.allOf(IsWinEnum.class));
		//MemuEnum
        model.add("MemuEnum",MemuEnum.wlzwcsxsxg);
		return "/views/online/onlineOrderUpList.vm";
	}

	@Post("getList")
	public String queryList(ComposeOnlineOrder composeOnlineOrder) {
		JSONObject jo = new JSONObject();
		
		List<ComposeOnlineOrder> objs = composeOnlineOrderDao.query(composeOnlineOrder);
		
		for(ComposeOnlineOrder obj:objs){
			obj.setPayWayName(obj.getPayWay()!=null?PayWayEnum.getName(obj.getPayWay()):"");
			obj.setStatusName(PayStatusEnum.getName(obj.getStatus()));
			obj.setIsCheckName(IsCheckEnum.getName(obj.getIsCheck()));
			obj.setIsExamName(IsExamEnum.getName(obj.getIsExam()));
			obj.setIsWinName(IsWinEnum.getName(obj.getIsWin()));
		}
		
		int size = composeOnlineOrderDao.querysize(composeOnlineOrder);
		jo.put("data", objs);
		jo.put("draw", composeOnlineOrder.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		return "@"+jo.toString();
	}
	
	@Get("createPage")
	public String createPage(Model model){
		List<Map> games = gameDao.asGameEnum(Integer.parseInt(GameTypeEnum.WLZZ.getValue()));
		model.add("Game", games);
		List<Map> areas = areaDao.asAreaEnum();
		model.add("Area", areas);
		List<Map> grades = gradeDao.asGradeEnum();
		model.add("Grade", grades);
		List<ComposeSubject> subjectlis = composeSubjectDAO.querySubjectAll();
		model.add("subjectlis", subjectlis);
		//MemuEnum
        model.add("MemuEnum",MemuEnum.wlzwcsxsgl);
		return "/views/online/onlineOrderDetail.vm";
	}
	
	@Post("create")
	public String create(ComposeOnlineOrder composeOnlineOrder, HttpServletRequest request){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		//验证身份信息
		List<Map> hasIdCards = composeOnlineOrderDao.getIDCard(composeOnlineOrder.getIdentityCard());
		if( hasIdCards != null && hasIdCards.size() > 0 ){                 //该身份证已报名
			Map idCard = hasIdCards.get(0);
			int status = Integer.parseInt(idCard.get("status").toString());
			if( status == PayStatusEnum.WFK.getValue() ){
				jo.put("returnFlag", Constants.returnObj.returnFlag_3);   //该身份证已报名,未付费,请前往个人中心>我的报名支付
				return "@" + jo.toString();
			}
			else if ( status == PayStatusEnum.YFK.getValue() ){
				jo.put("returnFlag", Constants.returnObj.returnFlag_repeat); //该身份证已付费,已报名,不要重复报名
				return "@" + jo.toString();
			}
		}
		
		Grade grade = gradeDao.queryById(Integer.parseInt(composeOnlineOrder.getGrade()));
		composeOnlineOrder.setGrade(grade.getName());
		Area area = areaDao.queryById(Integer.parseInt(composeOnlineOrder.getArea()));
		composeOnlineOrder.setArea(area.getName());
		composeOnlineOrder.setOrderNo(ReturnUUID.getOrderNo(ReturnUUID.getRandomNum()));
		composeOnlineOrder.setUserId(-1);
		composeOnlineOrder.setStatus(PayStatusEnum.YFK.getValue());
		composeOnlineOrder.setPayWay(PayWayEnum.XCJF.getValue());
		composeOnlineOrder.setSource(OrderSourceEnum.XCBM.getValue());
		composeOnlineOrder.setIsCheck(IsCheckEnum.WPG.getValue());
		composeOnlineOrder.setIsExam(IsExamEnum.WKS.getValue());
		composeOnlineOrder.setIsWin(IsWinEnum.WCZ.getValue());//未操作
		String account = returnSessionObj(request).getAccount();
		composeOnlineOrder.setCreateUser(account);
		composeOnlineOrder.setUpdateUser(account);
		composeOnlineOrder.setScore(0);
		composeOnlineOrder.setPayOrder("000000000");
		int id = composeOnlineOrderDao.create(composeOnlineOrder);
		
		//新建单据属于直接后台支付成功，那么直接添加参赛证号code
		String code = OrderHeardEnum.WL.getName()+ReturnUUID.NumFormat("0000000", id);
		composeOnlineOrderDao.updateById(id, code);
		
		return "@" + jo.toString();
	}
	
	@Get("edit")
	public String edit(Model model,@Param("id") int id){
		ComposeOnlineOrder composeOnlineOrder = composeOnlineOrderDao.queryById(id);
		model.add("Obj", composeOnlineOrder);
		Game game = gameDao.queryById(composeOnlineOrder.getGameId().intValue());
		model.add("Game", game);
		List<Map> areas = areaDao.asAreaEnum();
		model.add("Area", areas);
		for(Map area : areas){
			String areaName = area.get("name").toString();
			if( composeOnlineOrder.getArea().indexOf(areaName) >= 0){
				model.add("selArea", area.get("id"));
				break;
			}
		}
		
		List<Map> grades = gradeDao.asGradeEnum();
		model.add("Grade", grades);
		for(Map grade : grades){
			String gradeName = grade.get("name").toString();
			if( composeOnlineOrder.getGrade().indexOf(gradeName) >= 0){
				model.add("selGrade", grade.get("id"));
				break;
			}
		}
		//MemuEnum
        model.add("MemuEnum",MemuEnum.wlzwcsxsgl);
        List<ComposeSubject> subjectlis = composeSubjectDAO.querySubjectAll();
		model.add("subjectlis", subjectlis);
		return "/views/online/onlineOrderEdit.vm";
	}
	
	@Post("update")
	public String update(Model model,ComposeOnlineOrder composeOnlineOrder,HttpServletRequest request){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		Grade grade = gradeDao.queryById(Integer.parseInt(composeOnlineOrder.getGrade()));
		composeOnlineOrder.setGrade(grade.getName());
		Area area = areaDao.queryById(Integer.parseInt(composeOnlineOrder.getArea()));
		composeOnlineOrder.setArea(area.getName());
		String account = returnSessionObj(request).getAccount();
		composeOnlineOrder.setUpdateUser(account);
		composeOnlineOrderDao.update(composeOnlineOrder);
		
		return "@" + jo.toString();
	}
	
	/**
	 * 修改活动订单状态
	 * @param model
	 * @param id
	 * @return
	 */
	@Get("updateStatusById")
	public String updateStatusById(Model model,@Param("id") int id) {
		int status = PayStatusEnum.YFK.getValue();
		int source = OrderSourceEnum.XCBM.getValue();
		int payWay = PayWayEnum.XGDDZT.getValue();
		String payOrder = "000000000";
		String code = OrderHeardEnum.WL.getName()+ReturnUUID.NumFormat("0000000", id);
		
		composeOnlineOrderDao.updateStatusById(id, status, source, payWay, payOrder, code);
		return "r:updateShow";
	}
	
	/**
	 * 修改活动获奖状态-已获奖
	 * @param model
	 * @param id
	 * @return
	 */
	@Get("updateWinStatusById")
	public String updateWinStatusById(Model model,@Param("id") int id) {
		int isExam = IsExamEnum.YKS.getValue();
		int isCheck = IsCheckEnum.YPG.getValue();
		int isWin = IsWinEnum.YHJ.getValue();
		
		composeOnlineOrderDao.updateWinStatusById(id, isExam, isCheck, isWin);
		return "r:updateShow";
	}
	
	/**
	 * 修改活动获奖状态-未获奖
	 * @param model
	 * @param id
	 * @return
	 */
	@Get("updateNoWinStatusById")
	public String updateNoWinStatusById(Model model,@Param("id") int id) {
		int isExam = IsExamEnum.YKS.getValue();
		int isCheck = IsCheckEnum.YPG.getValue();
		int isWin = IsWinEnum.WHJ.getValue();
		
		composeOnlineOrderDao.updateWinStatusById(id, isExam, isCheck, isWin);
		return "r:updateShow";
	}
	
	@Get("deleteById")
	public String deleteById(Model model,@Param("id") int id){
		composeOnlineOrderDao.deleteById(id);
		return "r:show";
	}
}